package com.bzyd.cloud.controller;

import com.alibaba.fastjson.JSON;
import com.bzyd.cloud.feign.ProductService;
import com.bzyd.cloud.pojo.Order;
import com.bzyd.cloud.pojo.Product;
import com.bzyd.cloud.service.impl.OrderServiceImpl4;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * OrderController4，模拟RocketMQ事务消息
 * Created by Xianyue Dang on 2021/4/28
 */
@RestController
@Slf4j
public class OrderController4 {
    @Autowired
    private OrderServiceImpl4 orderServiceImpl4;
    @Autowired
    private ProductService productService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //准备买1件商品
//    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>接收到{}号商品的下单请求，这时候要调用商品微服务查询商品信息");

        //调用商品微服务，查询商品信息
        Product product = productService.findById(pid);
        log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));

        //下单(创建订单)
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderServiceImpl4.createOrderBefore(order);
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));

        return order;

    }


    @GetMapping("testTransactionMessage")
    public String testTransactionMessage() {
        Order order = new Order();
        order.setUid(123);
        order.setUsername("张三");
        order.setPid(100);
        order.setPname("测试事务消息商品名称");
        order.setPprice(100.0);
        order.setNumber(1);
        orderServiceImpl4.createOrderBefore(order);
        return "ok";
    }

}
